package com.zhora.ai.v1.controller;

import com.zhora.ai.main.UploadService;
import com.zhora.ai.v1.business.IAiLibraryBusinessService;
import com.zhora.ai.v1.business.IAiLibraryDocBusinessService;
import com.zhora.ai.v1.business.IAiLibraryDocSegmentBusinessService;
import com.zhora.ai.v1.business.impl.RagService;
import com.zhora.ai.v1.dto.AiLibraryDocListDTO;
import com.zhora.ai.v1.dto.AiLibraryDocSegmentListDTO;
import com.zhora.ai.v1.dto.AiLibraryListDTO;
import com.zhora.ai.v1.vo.AiLibraryDocListVO;
import com.zhora.ai.v1.vo.AiLibraryDocSegmentListVO;
import com.zhora.ai.v1.vo.AiLibraryListVO;
import com.zhora.common.domain.ResponseDTO;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Comparator;
import java.util.List;

@RestController
@RequestMapping("/knowledge/v1")
@RequiredArgsConstructor
@Slf4j
public class AiLibraryController {

    private final IAiLibraryBusinessService aiLibraryBusinessService;
    private final IAiLibraryDocBusinessService aiLibraryDocBusinessService;
    private final IAiLibraryDocSegmentBusinessService aiLibraryDocSegmentBusinessService;
    private final UploadService uploadService;
    private final RagService ragService;

    @Operation(summary = "rag知识库列表")
    @PostMapping("/libraries/list")
    public ResponseDTO<List<AiLibraryListDTO>> queryLibraries(@RequestBody AiLibraryListVO listVO) {
        return ResponseDTO.success(aiLibraryBusinessService.list(listVO));
    }

    @Operation(summary = "rag知识库文档列表")
    @PostMapping("/doc/list")
    public List<AiLibraryDocListDTO> queryLibraryDocs(@RequestBody AiLibraryDocListVO listVO) {
        return aiLibraryDocBusinessService.list(listVO).stream()
                .sorted(Comparator.comparing(AiLibraryDocListDTO::getId).reversed())
                .toList();
    }

    @Operation(summary = "rag知识库文档片段列表")
    @PostMapping("/segment/list")
    public List<AiLibraryDocSegmentListDTO> queryLibraryDocSegments(@RequestBody AiLibraryDocSegmentListVO listVO) {
        return aiLibraryDocSegmentBusinessService.list(listVO);
    }

    @Operation(summary = "删除知识库")
    @DeleteMapping("/library")
    public void deleteLibrary(@RequestParam Long libraryId) {
        ragService.deleteLibraryBy(libraryId);
    }

    @Operation(summary = "删除知识库文档")
    @DeleteMapping("/doc")
    public void deleteLibraryDoc(@RequestParam Long libraryDocId) {
        ragService.deleteDocBy(libraryDocId);
    }

    @Operation(summary = "上传知识库文档")
    @PostMapping(value = "/doc/upload", produces = MediaType.TEXT_PLAIN_VALUE)
    public String uploadLibraryDoc(
            @RequestPart("libraryId") String libraryId, @RequestPart("file") MultipartFile multipartFile)
            throws Exception {
        String objectName = uploadService.uploadLibraryDoc(multipartFile);
        Long libraryDocId =
                ragService.createLibraryDocBy(
                        Long.valueOf(libraryId), objectName, multipartFile.getOriginalFilename());

        ragService.embeddingAndCreateDocSegment(Long.valueOf(libraryId), libraryDocId, objectName);
        return objectName;
    }

}
